package com.kuyou.publiclibrary.base;

import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.kuyou.publiclibrary.utils.ScreenUtils;
import com.kuyou.publiclibrary.utils.SizeUtils;


public class ZRBaseActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    public void  translateFragment(Fragment fragment, int resId){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(resId, fragment);
        transaction.commit();

    }

    public void  addFragment(Fragment fragment, int resId){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(resId, fragment);
        transaction.commit();

    }


    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {

        if (isFitWindow()&&isScreenOriatationPortrait()) {
            if (toolbar != null) {
                //兼容屏幕上方打孔的手机，屏幕顶端打孔的手机的status bar的高度一般超过标准的22dip，所以根据这个条件判断是否是屏幕顶端打孔
                int statusBarHeight = ScreenUtils.getStatusBarHeight(this);
                int normalStatusBar = SizeUtils.dp2px(this, 22);
                if (statusBarHeight > normalStatusBar) {
                    toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + (statusBarHeight - normalStatusBar);
                }
                toolbar.setPadding(0, statusBarHeight, 0, 0);
                toolbar.requestLayout();
            }
        }
        super.setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setElevation(3);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * 是否是沉浸式状态栏
     * @return
     */
    protected boolean isFitWindow() {
        return true;
    }

    public  void initView(){}

    public  void initData(){}

    /**
     * 判断当前屏幕是否是竖屏
     * @return
     */
    public boolean isScreenOriatationPortrait() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

}
